package com.peiwangroup.messageservice.service;

import com.peiwangroup.messageservice.model.Order;
import com.peiwangroup.messageservice.model.WeChatMessage;
import com.peiwangroup.messageservice.repository.OrderRepository;
import org.springframework.stereotype.Service;
import java.util.regex.*;
import java.math.BigDecimal;
import java.util.Date;

@Service
public class MessageParserImpl implements MessageParser {
    private final OrderRepository orderRepository;
    
    public MessageParserImpl(OrderRepository orderRepository) {
        this.orderRepository = orderRepository;
    }
    
    private static final String ORDER_PATTERN = "派单：\\s*(.*?)\\s*接待：\\s*(.*?)\\s*陪陪：\\s*(.*?)\\s*老板：\\s*(.*?)\\s*类型：\\s*(.*?)\\s*时长：\\s*(.*?)\\s*总价：\\s*(.*?)\\s*团抽：\\s*(.*?)\\s*派抽：\\s*(.*?)\\s*接抽：\\s*(.*?)\\s*到手：\\s*(.*?)\\s*邀请人：\\s*(.*?)\\s*邀请人抽：\\s*(.*?)\\s*备注：\\s*(.*)";
    
    private Order parseOrderFromContent(String content) {
        Matcher matcher = Pattern.compile(ORDER_PATTERN).matcher(content);
        if (matcher.find()) {
            Order order = new Order();
            order.setDispatch(matcher.group(1));
            order.setReceptionist(matcher.group(2));
            order.setCompanion(matcher.group(3));
            order.setCustomer(matcher.group(4));
            order.setServiceType(matcher.group(5));
            order.setDuration(Integer.parseInt(matcher.group(6)));
            order.setTotalPrice(new BigDecimal(matcher.group(7)));
            order.setGroupCommission(new BigDecimal(matcher.group(8)));
            order.setDispatchCommission(new BigDecimal(matcher.group(9)));
            order.setReceptionCommission(new BigDecimal(matcher.group(10)));
            order.setFinalAmount(new BigDecimal(matcher.group(11)));
            order.setInviter(matcher.group(12));
            order.setInviterCommission(new BigDecimal(matcher.group(13)));
            order.setRemark(matcher.group(14));
            order.setCreateTime(new Date());
            return order;
        }
        return null;
    }
    
    @Override
    public WeChatMessage parse(String rawMessage) {
        if (rawMessage == null || rawMessage.trim().isEmpty()) {
            throw new IllegalArgumentException("Message cannot be null or empty");
        }
        
        Pattern pattern = Pattern.compile(ORDER_PATTERN);
        
        // 解析订单信息
        Order order = parseOrderFromContent(rawMessage);
        if (order != null) {
            order = orderRepository.save(order);
            WeChatMessage message = new WeChatMessage();
            message.setContent(rawMessage);
            message.setOrder(order);
            message.setMessageType("ORDER");
            return message;
        }
        
        // 如果不是订单消息，返回基本消息对象
        WeChatMessage message = new WeChatMessage();
        message.setContent(rawMessage);
        message.setMessageType("TEXT");
        return message;
    }
    
    private String generateOrderId() {
        return "ORDER_" + System.currentTimeMillis() + "_" + 
               String.format("%04d", (int)(Math.random() * 10000));
    }
}